<template>
  <view class="wrap">
    <uv-navbar :fixed="false" title="新建专栏" autoBack safeAreaInsetTop placeholder :border='false'>
      <template v-slot:left>
        <view class="navEditName nav-left">取消</view>
      </template>
      <template v-slot:right>
        <view class="navEditName nav-right" @click="tap">保存</view>
      </template>
    </uv-navbar>
    <view class="status"></view>
    <view class="input">
      <uv-input type="text" v-model="name" placeholder="请输入专栏名称" :maxlength='11' border="none" autoBlur fontSize='32rpx' color='#000000' placeholderStyle='font-size: 32rpx; color: #B8B8B8'></uv-input>
    </view>
    <view class="input" style="margin-top: 30rpx;">
      <uv-textarea v-model="desc" placeholder="请输入专栏介绍" height='300rpx' border='none'></uv-textarea>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { post } from '@/api';
import { useOrder } from '@/store';
import { toast } from '@/common';
import { onLoad } from '@dcloudio/uni-app';

let name = ref('');
let desc = ref('');
let ids = '';

let body = computed(() => {
  const store = useOrder();
  return uni.$uv.deepClone(store.group);
});
onLoad((option) => {
  ids = option.id;
  name.value = option.name;
  desc.value = option.desc;
})

function tap(){
  if(!name.value) return toast('请输入专栏名称');
  if(!desc.value) return toast('请输入专栏介绍');
  let url = '';
  const params = {};
  if(!ids){
    url = 'api/association_column/addAssociationColumn'
    const { id } = body.value;
    params.association_id = id;
  }else{
    url = 'api/association_column/editAssociationColumn';
    params.column_id = ids;
  }
  params.name = name.value;
  params.desc = desc.value;
  
  post(url, params).then(res => {
    if(res.code == 1){
      const content = ids ? '编辑成功' : '添加成功';
      toast(content, true);
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      }, 1000)
    }
  })
}
</script>

<style lang="scss" scoped>
  .input{
    padding: 30rpx;
    background-color: #fff;
  }
  .navEditName{
    font-weight: 400;
    font-size: 28rpx;
    &.nav-left{
      color: #737373;
    }
    &.nav-right{
      color: #E43222;
    }
  }
  .status{
    width: 100%;
    height: 20rpx;
    background-color: #F5F5F5;
  }
  .wrap{
    width: 100%;
    min-height: 100vh;
    background-color: #F5F5F5;
  }
</style>